﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccess;

namespace Bussiness {
    public class ProductBuss {
        public DataTable getProducts() {
            ProductRespository repository = new ProductRespository();
            return repository.getProducts();
        }
        public DataTable getProducts(int catID) {
            ProductRespository repository = new ProductRespository();
            return repository.getProducts(catID);
        }
        public DataTable getProduct(int productID) {
            ProductRespository repository = new ProductRespository();
            return repository.getProduct(productID);
        }
        public int add(String name, int catID, float price, String photoUrl, String description, String otherInfo, int Instock, float width, float heigth, float length, float weigth, int quantityAtLeast = 1) {
            ProductRespository repository = new ProductRespository();            
            return repository.add(name, catID, price, photoUrl, description, otherInfo, Instock, width, heigth, length, weigth, quantityAtLeast);
        }

        public bool update(int id, String name, int catID, float price, String photoUrl, String description, String otherInfo, int Instock, float width, float height, float length, float weight, int quantityAtLeast) {
            ProductRespository repository = new ProductRespository();
            return repository.update(id, name, catID, price, photoUrl, description, otherInfo, Instock, width, height, length, weight, quantityAtLeast);
        }

        public bool delete(int id) {
            ProductRespository repository = new ProductRespository();
            return repository.delete(id);
        }


        /*******ws******/
        public DataTable wsGetProducts(int catID) {
            ProductRespository repository = new ProductRespository();
            //ProductID, ProductName, ProductPrice, ProductPhotoURL, ProductDescription, UpdateDate, QuantityAtLeast, OtherInfo
            DataTable table = new DataTable("tblProducts");
            table.Columns.Add("ProductID");
            table.Columns.Add("ProductName");
            table.Columns.Add("ProductPrice");
            table.Columns.Add("ProductPhotoURL");
            table.Columns.Add("ProductDescription");
            table.Columns.Add("UpdateDate");
            table.Columns.Add("QuantityAtLeast");
            table.Columns.Add("OtherInfo");
            
            DataTable tblProducts= repository.wsGetProducts(catID);
            for (int i = 0; i < tblProducts.Rows.Count; i++) {
                DataRow row = table.NewRow();
                row[0] = tblProducts.Rows[i]["ProductID"];
                row[1] = tblProducts.Rows[i]["ProductName"];
                row[2] = tblProducts.Rows[i]["ProductPrice"];
                row[3] = "http://em07.somee.com" + tblProducts.Rows[i]["ProductPhotoURL"];
                row[4] = tblProducts.Rows[i]["ProductDescription"].ToString() + tblProducts.Rows[i]["OtherInfo"].ToString();
                row[5] = tblProducts.Rows[i]["UpdateDate"];
                row[6] = tblProducts.Rows[i]["QuantityAtLeast"];
                row[7] = tblProducts.Rows[i]["Length"].ToString() + "|" + tblProducts.Rows[i]["Width"].ToString() + "|" + tblProducts.Rows[i]["Height"].ToString() + "|" + tblProducts.Rows[i]["Weight"].ToString();

                table.Rows.Add(row);
            }
            return table;
        }
    }
}
